#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRegExpValidator>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->pushButton->setEnabled(false);

    ui->lineEdit_username->setMaxLength(20);
    ui->lineEdit_username->setPlaceholderText(QString("请输入用户名"));
    ui->lineEdit_username->setClearButtonEnabled(true);

    ui->lineEdit_password->setMaxLength(20);
    ui->lineEdit_password->setPlaceholderText(QString("请输入密码"));
    ui->lineEdit_password->setToolTip(QString("请输入不超过20位的密码"));
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);

    ui->lineEdit_checkpassword->setPlaceholderText(QString("请确认密码"));
    ui->lineEdit_checkpassword->setClearButtonEnabled(true);
    ui->lineEdit_checkpassword->setEchoMode(QLineEdit::Password);

    ui->radioButton_male->setChecked(true);

    ui->lineEdit_phonenum->setPlaceholderText(QString("请输入电话号码"));
    ui->lineEdit_phonenum->setClearButtonEnabled(true);
//    ui->lineEdit_phonenum->setInputMask("000-0000-0000");
    ui->lineEdit_phonenum->setValidator(new QRegExpValidator(QRegExp("^1\\d{10}$")));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QString gender = ui->radioButton_male->isChecked() ? "男" : "女";

    qDebug() << ui->lineEdit_username->text() <<
                ui->lineEdit_password->text() <<
                gender <<
                ui->lineEdit_phonenum->text();
}

void Widget::on_lineEdit_phonenum_textEdited(const QString &arg1)
{
    QString content = arg1;
    int pos = 0;

    if(ui->lineEdit_phonenum->validator()->validate(content, pos) == QValidator::Acceptable)
    {
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setEnabled(false);
    }
}

void Widget::on_lineEdit_password_textEdited(const QString &arg1)
{
    (void)arg1;
    this->check();
}

void Widget::on_lineEdit_checkpassword_textEdited(const QString &arg1)
{
    (void)arg1;
    this->check();
}

void Widget::check()
{
    const QString text1 = ui->lineEdit_password->text();
    const QString text2 = ui->lineEdit_checkpassword->text();

    if(text1.isEmpty() && text2.isEmpty())
    {
        ui->lineEdit_password->setToolTip(QString("密码为空"));
        ui->lineEdit_checkpassword->setToolTip(QString("密码为空"));
    }
    else if(text1 == text2)
    {
        ui->lineEdit_password->setToolTip(QString("密码相同"));
        ui->lineEdit_checkpassword->setToolTip(QString("密码相同"));
    }
    else
    {
        ui->lineEdit_password->setToolTip(QString("密码不同"));
        ui->lineEdit_checkpassword->setToolTip(QString("密码不同"));
    }
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    }
}

void Widget::on_checkBox_2_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_checkpassword->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_checkpassword->setEchoMode(QLineEdit::Password);
    }
}
